package cn.b.sky.admin.controller;

import cn.b.sky.admin.constants.SystemConstant;
import cn.b.sky.admin.enums.DwzCallbackType;
import cn.b.sky.admin.enums.DwzStatusCode;
import cn.b.sky.admin.model.BatchJobReport;
import cn.b.sky.admin.model.DWZResult;
import cn.b.sky.admin.model.Page;
import cn.b.sky.admin.model.TimeZone;
import cn.b.sky.admin.model.condition.TimeJobCondition;
import cn.b.sky.admin.service.AdminTimeZoneService;
import cn.b.sky.admin.service.AdminTimedJobService;
import cn.b.sky.schedule.model.TimedJobInfo;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <strong>Title : TimeJobController.java<br></strong>
 * <strong>Package : cn.b.sky.admin.controller<br></strong>
 * <strong>Description : </strong>管理系统定时任务执行<br> 
 * <strong>Create on : 2016-6-8 下午2:12:32<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/management/timejob/*")
public class AdminTimeJobController extends BaseController {

  @Resource
  private AdminTimedJobService adminTimedJobService;

  @Resource
  private AdminTimeZoneService adminTimeZoneService;

  @RequestMapping("timeJobList")
  public String timeJobList(Model model, @ModelAttribute("condition") TimeJobCondition condition, String navTabId) {
    Page timejob = adminTimedJobService.selectTimedJobList(condition);
    model.addAttribute("page", timejob);
    model.addAttribute(NAVTABID, navTabId);
    return "admin/job/job_list";
  }

  @RequestMapping("addTimeJob")
  public String addTimeJob(Model model, String navTabId) {
    List<TimeZone> timeZone = adminTimeZoneService.selectTimeZone();
    model.addAttribute("javaTimeZone", timeZone);
    model.addAttribute(NAVTABID, navTabId);
    return "admin/job/job_add";
  }

  @RequestMapping("editTimeJob")
  public String editTimeJob(Model model, String navTabId, String jobCode) {
    TimedJobInfo timedJob = adminTimedJobService.selectTimedJob(jobCode);
    List<TimeZone> timeZone = adminTimeZoneService.selectTimeZone();
    model.addAttribute("javaTimeZone", timeZone);
    model.addAttribute("timeJob", timedJob);
    model.addAttribute(NAVTABID, navTabId);
    return "admin/job/job_add";
  }

  @RequestMapping("saveOrUpdateJob")
  @ResponseBody
  public DWZResult saveOrUpdateJob(Model model, String navTabId, TimedJobInfo timedJobInfo, String jobCodeId,
                                   HttpServletRequest request) {
    if (timedJobInfo == null) {
      return ajaxError(SystemConstant.PARAMETERNULL);
    }
    if (!StringUtils.isBlank(jobCodeId)) {
      adminTimedJobService.updateTimedConfig(timedJobInfo);
    } else {
      if (adminTimedJobService.checkTimedJob(timedJobInfo.getJobCode())) {
        return ajaxError("作业编号已存在");
      }
      adminTimedJobService.saveTimedConfig(timedJobInfo);
    }
    return dialogAjaxDone(SystemConstant.DWZ_CODE_OK_MESSAGE, null, null, null, null);
  }

  @RequestMapping("chooseExpression")
  public String chooseExpression(Model model, String cronExpression) {
    if (!StringUtils.isBlank(cronExpression)) {
      model.addAttribute("cronExpression", cronExpression);
    }
    return "admin/job/job_expression";
  }

  @RequestMapping("doExecuteTimedJob")
  @ResponseBody
  public DWZResult doExecuteTimedJob(String navTabId, Model model, String jobCode) throws Exception {
    if (StringUtils.isBlank(jobCode)) {
      return ajaxError(SystemConstant.PARAMETERNULL);
    }
    TimedJobInfo timedJob = adminTimedJobService.selectTimedJob(jobCode);
    adminTimedJobService.doExecuteTimedJob(timedJob);
    return navTabAjaxDone(navTabId, null, null, null, SystemConstant.DWZ_CODE_OK_MESSAGE, null);
  }

  @RequestMapping("deleteTimeJob")
  @ResponseBody
  public DWZResult deleteTimeJob(Model model, String navTabId, String jobCodes, HttpServletRequest request) {
    if (StringUtils.isBlank(jobCodes)) {
      return ajaxError(SystemConstant.PARAMETERNULL);
    }
    String[] codeArr = jobCodes.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(codeArr.length);
    boolean hasFail = false;
    for (String jobCode : codeArr) {
      BatchJobReport report = adminTimedJobService.deleteTimedJob(jobCode);
      reports.add(report);
      if (!report.isSuccess()) {
        hasFail = true;
      }
    }
    if (!hasFail) {
      return navTabAjaxDone(navTabId, null, null, null, SystemConstant.DWZ_CODE_OK_MESSAGE, null);
    } else {
      HashMap<String, Object> map = new HashMap<String, Object>();
      map.put("title", "详细报告");
      map.put("data", reports);
      String jsonData = JSON.toJSONString(map);
      String forwardUrl = request.getContextPath() + "/manage/common/showReport";
      return getDWZJsonObject(DwzStatusCode.OK, null, null, null, DwzCallbackType.EMPTY, forwardUrl, null, jsonData);
    }
  }
}
